﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell.Interop;
using MsVsShell = Microsoft.VisualStudio.Shell;
using VsConstants = Microsoft.VisualStudio.VSConstants;
using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslShell = global::Microsoft.VisualStudio.Modeling.Shell;
using Microsoft.VisualStudio.Modeling.Shell;


namespace devtm.AutoMapper
{
	/// <summary>
	/// This PersistedWindowPane demonstrates the following features:
	///	 - Hosting a user control in a tool window
	///	 - Persistence (visible when VS starts based on state when VS closed)
	///	 - Tool window Toolbar
	///	 - Selection tracking (content of the Properties window is based on 
	///	   the selection in that window)
	/// 
	/// Tool windows are composed of a frame (provided by Visual Studio) and a
	/// pane (provided by the package implementer). The frame implements
	/// IVsWindowFrame while the pane implements IVsWindowPane.
	/// 
	/// PersistedWindowPane inherits the IVsWindowPane implementation from its
	/// base class (ToolWindowPane). PersistedWindowPane will host a .NET
	/// UserControl (PersistedWindowControl). The Package base class will
	/// get the user control by asking for the Window property on this class.
	/// </summary> 
	[Guid("C90FA85B-22D8-4F9F-9CA0-39769F8511C3")]
	[CLSCompliant(false)]
	public class AutomapperDetailsToolWindows : DslShell::ToolWindow
	{

		private ModelingDocView currentView;
		private ModelDetailExplorer control;
		//private bool ignoreSelectedObjectsChanges = false;

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="serviceProvider">Service provider.</param>
		public AutomapperDetailsToolWindows(global::System.IServiceProvider serviceProvider)
			: base(serviceProvider)
		{



			control = new ModelDetailExplorer(this);
			this.Content = control;
			control.InitializeComponent();


		}

		public void RefreshList(AutomapperDetailsToolWindows window)
		{

			window.control.RefreshList(this.currentView);

		}


		protected override void Initialize()
		{

			IMonitorSelectionService service = this.GetService(typeof(IMonitorSelectionService)) as IMonitorSelectionService;
			if (service != null)
			{

				this.currentView = service.CurrentDocumentView as ModelingDocView;
				
				service.DocumentChanged += new System.EventHandler<MonitorSelectionEventArgs>(service_DocumentChanged);
				service.DocumentWindowChanged += new System.EventHandler<MonitorSelectionEventArgs>(service_DocumentWindowChanged);
				service.SelectionChanged += new System.EventHandler<MonitorSelectionEventArgs>(service_SelectionChanged);
				service.WindowChanged += new System.EventHandler<MonitorSelectionEventArgs>(service_WindowChanged);

				// Delay initialization of the list until other tool windows have also had a chance to be
				// initialized
				control.Dispatcher.BeginInvoke((Action)delegate
				{
					// Populate the list view
					this.RefreshList(this);
				});

			}
		}

		void service_WindowChanged(object sender, MonitorSelectionEventArgs e)
		{
			WindowsChanged();
		}

		void service_SelectionChanged(object sender, MonitorSelectionEventArgs e)
		{
			WindowsChanged();          
		}

		void service_DocumentWindowChanged(object sender, MonitorSelectionEventArgs e)
		{
			WindowsChanged();
		}

		void service_DocumentChanged(object sender, MonitorSelectionEventArgs e)
		{
			WindowsChanged();
		}

		private void WindowsChanged()
		{

			IMonitorSelectionService service = this.GetService(typeof(IMonitorSelectionService)) as IMonitorSelectionService;

			if (service != null)
			{

				ModelingDocView cc = service.CurrentDocumentView as ModelingDocView;
				if (cc != null)
					if (cc != this.currentView)
					{

						this.currentView = cc;

						// Delay initialization of the list until other tool windows have also had a chance to be initialized
						control.Dispatcher.BeginInvoke((Action)delegate
						{
							// Populate the list view
							this.RefreshList(this);
						});

					}

			}
		}


		/// <summary>
		/// Specifies a resource string that appears on the tool window title bar.
		/// </summary>
		public override string WindowTitle
		{
			get
			{
				//global::System.Resources.ResourceManager resourceManager = global::devtm.AutoMapper.AutoMapperDomainModel.SingletonResourceManager;
				return "Automapper Details"; // resourceManager.GetString("ModelExplorerTitle");
			}
		}
		
		/// <summary>
		/// Resource ID from VSPackage.resx containing the Model Explorer icon.
		/// </summary>
		protected override int BitmapResource
		{
			get
			{
				return 104;
			}
		}

		/// <summary>
		/// Index of tool window icon in BitmapResource.
		/// </summary>
		protected override int BitmapIndex
		{
			get { return 0; }
		}
		
		/// <summary>
		/// Creates the model explorer to be hosted in the window.
		/// </summary>
		/// <returns>ModelExplorerTreeContainer</returns>
		protected ModelDetailExplorer CreateTreeContainer()
		{
			return new ModelDetailExplorer(this);
		}
		

	}

}
